<template>
  <div class="table-container">
    <catch-table :form-create="formCreate" v-bind="table"/>

    <el-dialog title="项目详情" :visible.sync="dialogVisible" width="60%" style="margin-bottom: 100px" :before-close="handleClose">
      <el-descriptions border title="基本信息" :column="2">
        <el-descriptions-item label="监理业务类型">{{ e.need_type }}</el-descriptions-item>
        <el-descriptions-item label="预估监理费总额">{{ e.need_amount }}</el-descriptions-item>
        <el-descriptions-item label="监理要求">{{ e.need_yao_qiu }}</el-descriptions-item>
        <el-descriptions-item label="监理时间">{{ e.need_time }}</el-descriptions-item>
        <el-descriptions-item label="付款方式">{{ e.need_pay_type }}</el-descriptions-item>
        <el-descriptions-item label="项目所在地">{{ e.need_city_name }}</el-descriptions-item>
        <el-descriptions-item label="对监理公司要求">{{ e.company_yao_qiu }}</el-descriptions-item>
        <el-descriptions-item label="本信息截止有效期">{{ e.need_ent_date }}</el-descriptions-item>
      </el-descriptions>

      <el-descriptions border title="联系信息" :column="2" style="margin-top: 30px">
        <el-descriptions-item label="需求单位">{{ e.company_name }}</el-descriptions-item>
        <el-descriptions-item label="联系人">{{ e.linkman_realname }}</el-descriptions-item>
        <el-descriptions-item label="职务">{{ e.linkman_duty }}</el-descriptions-item>
        <el-descriptions-item label="电话">{{ e.linkman_mobile }}</el-descriptions-item>
      </el-descriptions>


      <el-descriptions v-if="e.status != 1" border title="审核" :column="2" style="margin-top: 30px;">
        <el-descriptions-item label="审核状态">{{ e.status_name }}</el-descriptions-item>
        <el-descriptions-item label="审核备注">{{ e.status_note }}</el-descriptions-item>
      </el-descriptions>

      <div v-if="e.status == 1" style="margin: 30px;">
        <el-input type="textarea" :rows="6" placeholder="备注" v-model="e.status_note">
        </el-input>
        <div style="display: flex;justify-content: center;margin-top: 20px">
          <el-button type="danger" @click="statusChange(e.id,3)">拒绝</el-button>
          <el-button type="primary" style="margin-left: 150px;" @click="statusChange(e.id,2)">通过</el-button>
        </div>
      </div>
    </el-dialog>

    <!-- 公司 -->
    <el-dialog title="公司" :visible.sync="matchDialogVisible" width="60%" style="margin-bottom: 100px" :before-close="handleCloseMatch">
      <template>
        <el-table ref="multipleTable" :data="tableData" tooltip-effect="dark" style="width: 100%" @selection-change="handleSelectionChange">
          <!--          <el-table-column type="selection" width="55"></el-table-column>-->
          <el-table-column prop="company_name" label="公司名" width="120"></el-table-column>
          <el-table-column prop="city_name" label="地址"></el-table-column>
        </el-table>
      </template>
      <!--      <el-button type="primary" @click="matchUser">提交</el-button>-->
    </el-dialog>
  </div>
</template>

<script>
import renderTable from '@/views/render-table-form'

export default {
  mixins: [renderTable],
  data() {
    return {
      tableData: [],
      multipleSelection: [],
      multipleSelection_id: [],
      need_audit: 1,
      status_note: '',
      tableFrom: '',
      e: { status_note: '' },
      dialogVisible: false,
      need_id: 0,
      matchDialogVisible: false
    }
  },

  mounted() {
  },

  methods: {
    toggleSelection(rows) {
      if (rows) {
        rows.forEach((row) => {
          this.$refs.multipleTable.toggleRowSelection(row)
        })
      } else {
        this.$refs.multipleTable.clearSelection()
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val

      // 关键修复：每次选择变化时先清空数组，再重新添加当前选中的ID
      this.multipleSelection_id = []

      // 遍历当前选中的行，收集ID
      this.multipleSelection.forEach((item) => {
        this.multipleSelection_id.push(item.id)
      })

      // 打印当前选中的所有ID
      console.log('当前选中的公司ID：', this.multipleSelection_id)
    },

    matchUser() {
      this.$http({
        url: 'Need/matchUser',
        method: 'POST',
        data: {
          need_id: this.need_id,
          user_company_id: this.multipleSelection_id
        }
      }).then((r) => {
        this.matchDialogVisible = false
        // this.tableData = r.data
      })
    },

    indexCompanyByNeed(need_id) {
      this.need_id = need_id
      this.tableData = []
      this.$http({
        url: '/UserCompany/indexByNeed',
        method: 'POST',
        data: {
          need_id: need_id
        }
      }).then((r) => {
        this.tableData = r.data
      })
    },

    beforeSubmit(row) {
      if (row.form.parent_id instanceof Array) {
        row.form.parent_id = row.form.parent_id.length > 0 ? row.form.parent_id.pop() : 0
      }
      return row
    },
    handleView(e) {
      this.e = e
      this.dialogVisible = true
    },
    handleCloseMatch(e) {
      this.e = e
      this.matchDialogVisible = false
    },

    handleMatchUserCompany(e) {
      this.e = e
      this.indexCompanyByNeed(e.id)
      this.matchDialogVisible = true
    },

    statusChange(id, status) {
      this.$http({
        url: '/Need/audit',
        method: 'POST',
        data: {
          id: id,
          status: status,
          status_note: this.e.status_note
        }
      }).then((r) => {
        this.dialogVisible = false
        this.$message.success('操作成功')
        this.$refs[this.table.ref].getList()
      })
    },

    zidingyi(e) {
      console.log(e)
    }
  }
}
</script>
